/**
 * Copyright  (c) 2009-2013 XLab,  Org. 
 * All  rights  reserved. 
 */
package org.xlab.baseframework.dao.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.xlab.baseframework.dao.api.ILogicRemoveDAO;
import org.xlab.baseframework.domain.ILogicalRemoveSupportedDomain;
import org.xlab.baseframework.domain.enumerate.EnumDeletedIndentifying;
import org.xlab.baseframework.exception.UnsupportedOperationException;


/**
 * <p>
 * ILogicRemoveDAO接口的一个默认实现类，默认实现是按照改变对象的“deletedInd”值进行逻辑删除的，如果该对象没有“deletedInd
 * ”属性，将会抛出UnsupportedOperationException异常
 * </p>
 * 
 * @author makubex Email:makubexnet@gmail.com
 * @version 1.0.0 Sep 7, 2010
 * @since 1.0.0
 */
@Repository
public abstract class LogicRemoveDAOImpl<T extends ILogicalRemoveSupportedDomain, PK extends Serializable> extends
		BaseDAOImpl<T, PK> implements ILogicRemoveDAO<T, PK> {

	private static final Logger log = Logger
			.getLogger(LogicRemoveDAOImpl.class);

	@SuppressWarnings("unchecked")
	private void executeUpdate(ILogicalRemoveSupportedDomain instance,
			EnumDeletedIndentifying deletedInd) {
		instance.setDeletedInd(deletedInd);
		super.update((T) instance);
	}

	/**
	 * @see org.xlab.baseframework.dao.api.ILogicRemoveDAO#logicRemove(java.lang.Object)
	 */
	public void logicRemove(T instance) throws UnsupportedOperationException {
		if (instance instanceof ILogicalRemoveSupportedDomain) {
			ILogicalRemoveSupportedDomain removeable = (ILogicalRemoveSupportedDomain) instance;
			this.executeUpdate(removeable, EnumDeletedIndentifying.Y);
		} else {
			String message = super.messageSource.getMessage(
					"message.usoexp.unsupported.operation", new Object[] {
							super.tClass.getName(), "Logic Remove" },
					Locale.getDefault());
			log.error(message);
			throw new UnsupportedOperationException(message);
		}
	}

	/**
	 * @see org.xlab.baseframework.dao.api.ILogicRemoveDAO#logicRemoveAll(java.util.Collection)
	 */
	public void logicRemoveAll(Collection<T> instanceCollection)
			throws UnsupportedOperationException {
		for (T instance : instanceCollection) {
			if (instance instanceof ILogicalRemoveSupportedDomain) {
				ILogicalRemoveSupportedDomain removeable = (ILogicalRemoveSupportedDomain) instance;
				this.executeUpdate(removeable, EnumDeletedIndentifying.Y);
			} else {
				String message = super.messageSource.getMessage(
						"message.usoexp.unsupported.operation", new Object[] {
								super.tClass.getName(), "Logic Remove" },
						Locale.getDefault());
				log.error(message);
				throw new UnsupportedOperationException(message);
			}
		}
	}

	/**
	 * @see org.xlab.baseframework.dao.api.ILogicRemoveDAO#logicRestore(java.lang.Object)
	 */
	public void logicRestore(T instance) throws UnsupportedOperationException {
		if (instance instanceof ILogicalRemoveSupportedDomain) {
			ILogicalRemoveSupportedDomain removeable = (ILogicalRemoveSupportedDomain) instance;
			this.executeUpdate(removeable, EnumDeletedIndentifying.N);
		} else {
			String message = super.messageSource.getMessage(
					"message.usoexp.unsupported.operation", new Object[] {
							super.tClass.getName(), "Logic Restore" },
					Locale.getDefault());
			log.error(message);
			throw new UnsupportedOperationException(message);
		}
	}

	/**
	 * @see org.xlab.baseframework.dao.api.ILogicRemoveDAO#logicRestoreAll(java.util.Collection)
	 */
	public void logicRestoreAll(Collection<T> instanceCollection)
			throws UnsupportedOperationException {
		for (T instance : instanceCollection) {
			if (instance instanceof ILogicalRemoveSupportedDomain) {
				ILogicalRemoveSupportedDomain removeable = (ILogicalRemoveSupportedDomain) instance;
				this.executeUpdate(removeable, EnumDeletedIndentifying.N);
			} else {
				String message = super.messageSource.getMessage(
						"message.usoexp.unsupported.operation", new Object[] {
								super.tClass.getName(), "Logic Restore" },
						Locale.getDefault());
				log.error(message);
				throw new UnsupportedOperationException(message);
			}
		}

	}
}
